<?php


namespace app\common\controller;


use app\common\logic\BaseLogic;
use app\common\validate\BaseValidate;
use core\traits\ApiResponse;
use core\utils\Form;
use support\Response;

class BaseAdminController
{
    use ApiResponse;

    protected $noNeedLogin = []; //是否不需要登录
    //是否不需要鉴权
    protected $noNeedRight = [
        'read',
    ];

    protected Form $form;

    protected $field = []; //列表查询字段 默认全部
    protected $except = false; // 只查询field 还是排除field  true的时候是排除field字段的其他字段



    /**
     * 逻辑层注入
     * @var BaseLogic
     */
    protected $logic;

    /**
     * 验证器注入
     * @var BaseValidate
     */
    protected $validate;

    protected $idKey = "id";
    protected $idsKey = 'ids';

    protected $statusKey = "status";

    const ADD = 'add';
    const UPDATE = 'update';
    const DELETE = 'delete';

    /**
     * 构造方法
     * @access public
     */
    public function __construct()
    {
        // 控制器初始化
        $this->init();

        $this->form = request()->getForm();
    }

    protected function init()
    {
        $this->logic && $this->logic->init();
    }

    protected function searchWhere()
    {
        return [];
    }

    /**
     * 默认排序
     * @return array
     */
    protected function listOrder()
    {
        return [];
    }

    public function list()
    {
        $query = $this->logic->search($this->searchWhere());
        $query->order($this->listOrder());
        if ($this->except) {
            $query->withoutField($this->field);
        } else {
            $query->field($this->field ?: '*');
        }
        $data = $this->logic->getPageList($query);
        return $this->success($data);
    }


    /**
     * 保存数据
     * @return Response
     */
    public function add()
    {
        $data = input();
        $this->beforeAddOrEdit("add", $data);
        if ($this->validate) {
            if (!$this->validate->scene('add')->check($data)) {
                return $this->error($this->validate->getError());
            }
        }
        $result = $this->logic->save($data);
        if ($result) {
            $this->afterUpdate(self::ADD, $data);
            return $this->success();
        } else {
            return $this->error();
        }
    }

    /**
     * 修改数据
     * @return Response
     */
    public function update()
    {
        $id = request()->input($this->idKey);
        $data = input();
        $this->beforeAddOrEdit("update", $data);
        if ($this->validate) {
            if (!$this->validate->scene('update')->check($data)) {
                return $this->error($this->validate->getError());
            }
        }

        $result = $this->logic->update($data, [$this->idKey => $id]);
        if ($result) {
            $this->afterUpdate(self::UPDATE, $data);
            return $this->success();
        } else {
            return $this->error();
        }
    }

    /**
     * 修改状态
     * @return Response
     */
    public function changeStatus()
    {
        $id = request()->input($this->idKey);
        $status = request()->input($this->statusKey);

        $result = $this->logic->where($this->idKey, $id)->update([$this->statusKey => $status]);
        if ($result) {
            $data = input();
            $this->afterUpdate("changeStatus", $data);
            return $this->success();
        } else {
            return $this->error();
        }
    }

    /**
     * 删除数据
     * @return Response
     */
    public function remove()
    {
        $ids = request()->input($this->idsKey, [request()->input($this->idKey)]);
        if (!empty($ids)) {
            $this->logic->where([
                [$this->logic->getModel()->getPk(), 'in', $ids]
            ])->delete();
            $data = [
                $this->idsKey => $ids
            ];
            $this->afterUpdate("delete", $data);
            return $this->success();
        } else {
            return $this->error('参数错误，请检查');
        }
    }

    /**
     * 读取信息
     * @return Response
     */
    public function read()
    {
        if ($this->validate) {
            if (!$this->validate->scene('id')->check(input())) {
                return $this->error($this->validate->getError());
            }
        }
        $model = $this->logic->find(input($this->idKey));
        if ($model) {
            $data = $model->toArray();
            return $this->success($data);
        } else {
            return $this->error('未查找到信息');
        }
    }

    /**
     * 新增或者修改之前操作,用于修改请求参数
     * @param $type
     * @param $data
     */
    public function beforeAddOrEdit($type, &$data)
    {

    }

    /**
     * 新增或者修改之后操作,用于操作比如删除缓存之内的操作
     * @param $type
     * @param $data
     */
    public function afterUpdate($type, &$data)
    {

    }

    /**
     * read接口回显 数据处理
     * @param $data
     */
    public function afterRead(&$data)
    {

    }
}